﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Import;
using System.Web;
using System.Web.Script.Serialization;
using Inventory.Domain;

namespace ImportRottenTomatoes
{
    public class ImportMovie : IImport<Inventory.Domain.Models.Movie>
    {
        public ImportMovie(string key, string url)
        {
            Key = key;
            Url = url;
        }

        public List<Inventory.Domain.Models.Movie> Search(string term, int pageLimit)
        {
            var movies = new List<Inventory.Domain.Models.Movie>();

            using (var wclient = new WebClient())
            {
                //movie search
                string url = Url + "/movies.json?apikey=" + Key + "&q=" + term + "&page_limit=" + pageLimit;
                wclient.Headers.Add("Content-Type", "application/json");
                var response = wclient.DownloadString(url);

                var ser = new JavaScriptSerializer();
                var movSearch = ser.Deserialize<MovieSearch>(response);
                foreach (var mov in movSearch.movies)
                {
                    movies.Add(ConvertToMovie(mov));
                }

            }
            return movies;
        }

        private Inventory.Domain.Models.Movie ConvertToMovie(Movie mov)
        {
            var movie = new Inventory.Domain.Models.Movie();
            movie.RtId = int.Parse(mov.Id);
            movie.Title = mov.Title;
            movie.ReleaseYear = mov.Year;
            movie.SynopsisLong = mov.synopsis;
            movie.Runtime = mov.Runtime;

            return movie;
        }
        

        public string Key {get;set;}
        public string Url {get;set;}


        public Inventory.Domain.Models.Movie GetMovie(int id)
        {
            var movie = new Inventory.Domain.Models.Movie();
            using (var wclient = new WebClient())
            {
                var url = Url + "/movies/" + id + ".json?apikey=" + Key;
                wclient.Headers.Add("Content-Type", "application/json");
                var response = wclient.DownloadString(url);

                var ser = new JavaScriptSerializer();
                var mov = ser.Deserialize<Movie>(response);

                movie = ConvertToMovie(mov);
            }
            return movie;
        }
    }

    public class Release_Dates
    {
        public string theater { get; set; }
        public string dvd { get; set; }
    }

    public class Ratings
    {
        public string critics_rating { get; set; }
        public int critics_score { get; set; }
        public string audience_rating { get; set; }
        public int audience_score { get; set; }
    }

    public class Posters
    {
        public string Thumbnail { get; set; }
        public string Profile { get; set; }
        public string Detailed { get; set; }
        public string Original { get; set; }
    }

    public class Abridged_Cast
    {
        public string Name { get; set; }
        public string Id { get; set; }
        public string[] Characters { get; set; }
    }

    public class Abridged_Directors
    {
        public string Name { get; set; }
    }

    public class Alternate_Ids
    {
        public string Imdb { get; set; }
    }

    public class Links
    {
        public string Self { get; set; }
        public string Alternate { get; set; }
        public string Cast { get; set; }
        public string Clips { get; set; }
        public string Reviews { get; set; }
        public string Similar { get; set; }
    }

    public class Movie
    {
        public string Id { get; set; }
        public string Title { get; set; }
        public int? Year { get; set; }
        public string[] Genres { get; set; }
        public string Mpaa_Rating { get; set; }
        public int? Runtime { get; set; }
        public string Critics_Consensus { get; set; }
        public Release_Dates release_dates { get; set; }
        public Ratings ratings { get; set; }
        public string synopsis { get; set; }
        public Posters posters { get; set; }
        public Abridged_Cast[] abridged_cast { get; set; }
        public string studio { get; set; }
        public Alternate_Ids alternate_ids { get; set; }
        public Links links { get; set; }
    }

    public class Links2
    {
        public string self { get; set; }
        public string next { get; set; }
    }

    public class MovieSearch
    {
        public int? total { get; set; }
        public Movie[] movies { get; set; }
        public Links2 links { get; set; }
        public string link_template { get; set; }
    }
}
